package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.main.X3BottomAloneThingAdapter;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.main.AloneThingBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3Utils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class X3BottomAloneThingDialog extends X3BaseBottomSheetDialog implements View.OnClickListener, X3BottomAloneThingAdapter.IOnSelectThing {

    private TextView tv_cancel;
    private RecyclerView rlv_thing;
    private TextView tv_affirm;
    private TextView tv_empty;
    private TextView tv_add;

    private X3BottomAloneThingAdapter thingAdapter;

    private List<String> answerList = new ArrayList<>();

    private long receiveBaseId;
    private X3BottomAddThingDialog addThingDialog;

    public X3BottomAloneThingDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_cancel = getView(R.id.tv_cancel);
        tv_empty = getView(R.id.tv_empty);
        rlv_thing = getView(R.id.rlv_thing);
        tv_affirm = getView(R.id.tv_affirm);
        tv_add = getView(R.id.tv_add);

        tv_cancel.setOnClickListener(this);
        tv_affirm.setOnClickListener(this);
        tv_add.setOnClickListener(this);

        rlv_thing.setLayoutManager(new LinearLayoutManager(mContext));
        thingAdapter = new X3BottomAloneThingAdapter(mContext);
        rlv_thing.setAdapter(thingAdapter);
        thingAdapter.setiOnSelectThing(this);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_bottom_alone_thing_dialog;
    }

    @Override
    public void show() {
        super.show();
    }

    /**
     * 请求物品清单数据
     */
    public void setQuestionSystemSn(String answer,long receiveBaseId) {
        this.receiveBaseId = receiveBaseId;
        if (X3StringUtils.isEmpty(answer)) {
            tv_affirm.setText(mContext.getString(R.string.x3_affirm_no_thing));
        }else {
            tv_affirm.setText(mContext.getString(R.string.x3_keep));
        }
        NetWorkHttp.getApi().getBrandFirstThing(receiveBaseId)
                .compose(((X3IBaseView)mContext).bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<List<AloneThingBean>>() {
                    @Override
                    protected void onDone(List<AloneThingBean> mList) {
                        if (X3StringUtils.isListEmpty(mList)) {
                            tv_add.setText(mContext.getString(R.string.x3_add));
                            tv_empty.setVisibility(View.VISIBLE);
                            return;
                        }
                        tv_add.setText(mContext.getString(R.string.x3_add_other));
                        tv_empty.setVisibility(View.GONE);

                        if (thingAdapter != null) {
                            thingAdapter.changeAloneThing(mList,answer);
                        }
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_cancel) {
            dismiss();
        } else if (id == R.id.tv_affirm) {
            if (!X3Utils.isFastClick()) {
                return;
            }
            dismiss();
            NetWorkHttp.getApi().saveOrUpdateThing(receiveBaseId, answerList)
                    .compose(RxSchedulersHelper.defaultComposeRequest())
                    .compose(((X3IBaseView) mContext).bindToLife())
                    .subscribe(new HttpCallBack<Object>() {
                        @Override
                        protected void onDone(Object o) {
                            if (iOnRefresh != null) {
                                iOnRefresh.iOnRefreshData();
                            }
                        }

                        @Override
                        protected String LoadingMessage() {
                            return null;
                        }

                        @Override
                        protected void showError(String error) {

                        }
                    });
        } else if (id == R.id.tv_add) {// 添加其他
            if (addThingDialog == null) {
                addThingDialog = new X3BottomAddThingDialog(mContext);
            }
            addThingDialog.show();
            addThingDialog.setiOnAddThing(thing -> {
                // 获取添加的物品 添加到集合当中
                tv_empty.setVisibility(View.GONE);
                if (thingAdapter != null) {
                    thingAdapter.addAloneThing(thing);
                }
            });
        }
    }

    IOnRefresh iOnRefresh;

    public void setiOnRefresh(IOnRefresh iOnRefresh) {
        this.iOnRefresh = iOnRefresh;
    }

    @Override
    public void iOnSelectThing(List<String> nameList) {
        this.answerList = nameList;
        if (X3StringUtils.isListEmpty(answerList)) {
            tv_affirm.setText(mContext.getString(R.string.x3_affirm_no_thing));

        }else {
            tv_affirm.setText(mContext.getString(R.string.x3_keep));
        }
    }

    /**
     * 刷新数据
     */
    public interface IOnRefresh {
        void iOnRefreshData();
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("初检信息物品清单弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("初检信息物品清单弹窗");
    }
}
